package com.zjj.wangluo.lianxi.tcp04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 *
 * 读
 */
public class ReadThread implements Runnable {

    private Socket socket;

    public ReadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                ){
            String str = null;
            while ((str = br.readLine()) != null) {
                if(str.equals("exit")){
                    System.out.println(socket.getRemoteSocketAddress()+"已下线！");
                    break;
                }else{
                    System.out.println(socket.getRemoteSocketAddress()+"说："+str);
                }
            }
        } catch (SocketException e){
            System.out.println("拜拜了，我走了");
        } catch(IOException e) {
            e.printStackTrace();
        }finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
